package com.hw.hcit.feature;


import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.graphics.Color;
import android.opengl.Visibility;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.content.ContentResolver;
import android.provider.Settings;
import com.hw.hcit.R;
import com.hw.hcit.conf.Config;
import com.hw.hcit.conf.JCtrlCallback;
import com.hw.hcit.conf.JTestItem;
import com.hw.hcit.conf.JTestItemCallback;

public class  LCDTestItem extends JTestItem implements OnClickListener {
    private int num = 0 ;
    private ImageView[] images = new ImageView [7];
    private boolean powerClicked = false;
    private String TAG = "LCDTestItem";
    private RelativeLayout relativeLayout;
    private Button mButton1,mButton2;
    private static int brightness;
    public  LCDTestItem() {
        super();    }

    @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
 {
        Log.d(TAG, "----------->++4");
        View rootView = inflater.inflate(R.layout.led_test, container, false);
        images[0] = (ImageView) rootView.findViewById(R.id.parent_0);
        images[1] = (ImageView) rootView.findViewById(R.id.led_1);
        images[2] = (ImageView) rootView.findViewById(R.id.led_2);
        images[3] = (ImageView) rootView.findViewById(R.id.led_3);
        images[4] = (ImageView) rootView.findViewById(R.id.led_4);
        images[5] = (ImageView) rootView.findViewById(R.id.led_5);
        images[6] = (ImageView) rootView.findViewById(R.id.led_6);
        relativeLayout = (RelativeLayout) rootView.findViewById(R.id.confirm);
        mButton1 = (Button) rootView.findViewById(R.id.button1);
        mButton2 = (Button) rootView.findViewById(R.id.button2);
        mButton1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(Config.CURR_PROJECT.isAuto()){
                    if (!Config.ageingTested) {
                        rewriteOneByteOfNv2499(3, 'P');

                    } else {
                        rewriteOneByteOfNv2499(34, 'P');
                    }
                }
                 testFinish(true, 1, true);
                showNavigationBar();
            }
        });
        mButton2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(Config.CURR_PROJECT.isAuto()){
                    if (!Config.ageingTested) {
                        rewriteOneByteOfNv2499(3, 'F');

                    } else {
                        rewriteOneByteOfNv2499(34, 'F');
                    }
                    
                }
                testFinish(false, 1, false);
                showNavigationBar();
            }
        });

        startTest();
        return rootView;
    }

    @Override
    public boolean getResult() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public String getResultInfo() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void startTest() {
        // TODO Auto-generated method stub
        //start to test
    }

    @Override
    public void stopTest() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void refresh()
    {
        
    }

    
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //nv write
        //Config.curr_pro.isAuto()
        //Config.curr_pro.getType()
        //Config.CURR_PROJECT.getType()
    }
    
    /* 
     * @see com.hw.hcit.conf.JTestItem#dispatchKeyEvent(android.view.KeyEvent)
     * For five kinds of colors
     */
    @Override
   public boolean dispatchKeyEvent(KeyEvent event) {
      // TODO Auto-generated method stub
      Log.d(TAG, "----------->++1");
      Log.d(TAG, "----------->++action" + event.getKeyCode());
      switch (event.getKeyCode()) {
      case KeyEvent.KEYCODE_VOLUME_DOWN:
          dissmissPowerClicked();
          if(powerClicked){
              
              return true;
          }
         Log.d(TAG, "----------->++num+" + num);
         ++num;
         switchColor(num);
         Log.d(TAG, "afterSwitch----------->++num+" + num);
         if (num > 6) {
             num = 0;
         }
         break;

      case KeyEvent.KEYCODE_VOLUME_UP:
          dissmissPowerClicked();
         num--;
         if (num < 0) {
             num = 0;
         }
         Log.d(TAG, "----------->++num+" + num);

         break;
/*      case  KeyEvent.KEYCODE_POWER:
          Log.d(TAG,"lcd------------>power");
            powerClicked = true;
            showPowerClicked();
         break;*/
      default:
         break;
      }
      return true;
   }
    
    public void hideNavigationBar() {
        int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
            | View.SYSTEM_UI_FLAG_FULLSCREEN; // hide status bar

        if( android.os.Build.VERSION.SDK_INT >= 19 ){ 
            uiFlags |= 0x00001000;    //SYSTEM_UI_FLAG_IMMERSIVE_STICKY: hide navigation bars - compatibility: building API level is lower thatn 19, use magic number directly for higher API target level
        } else {
            uiFlags |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
        }

        Config.GetActivity().getWindow().getDecorView().setSystemUiVisibility(uiFlags);
    }

    private void showNavigationBar() {
        Config.GetActivity().getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }

    /* (non-Javadoc)
     * @see android.app.Fragment#onResume()
     */
    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        hideNavigationBar();
        /*15-10-22 gaoshenpan fix change brightnessMode to manul begin*/
        try {
         brightness=Settings.System.getInt(Config.GetActivity().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
            Log.d(TAG, "brightness-onresume1-->"+brightness);
            Settings.System.putInt(Config.GetActivity().getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
            /*16-1-29 gaoshenpan fix change brightness to 220 begin*/
            Settings.System.putInt(Config.GetActivity().getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS,220);
            /*16-1-29 gaoshenpan fix change brightness to 220 end*/
            Log.d(TAG, "brightnessMode-onResume-->"+Settings.System.getInt(Config.GetActivity().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE));
            Log.d(TAG, "brightness-onresume2-->"+Settings.System.getInt(Config.GetActivity().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS));
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        /*15-10-22 gaoshenpan fix change brightnessMode to manul end*/
        /*15-10-21 gaoshenpan fix full screen not show navigationbar begin*/
        Settings.Global.putInt(Config.GetActivity().getContentResolver(), "mmi_test_fullscreen", 1);
        /*15-10-21 gaoshenpan fix full screen not show navigationbar end*/
        images[0].setVisibility(View.VISIBLE);
        powerClicked = false;
    }
    
    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        /*15-10-22 gaoshenpan fix resetbrightnessMode to auto onpause begin*/
        Settings.System.putInt(Config.GetActivity().getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
           Settings.System.putInt(Config.GetActivity().getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS,brightness);
        try {
             Log.d(TAG, "brightnessMode-onpause-->"+Settings.System.getInt(Config.GetActivity().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE));
             Log.d(TAG, "brightness-onpause-->"+Settings.System.getInt(Config.GetActivity().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS));
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        /*15-10-22 gaoshenpan fix resetbrightnessMode to auto onpause end*/
         Log.d(TAG, "----------->++onPause+");
        /*15-10-21 gaoshenpan  show navigationbar begin*/
        Settings.Global.putInt(Config.GetActivity().getContentResolver(), "mmi_test_fullscreen", 0);
        /*15-10-21 gaoshenpan  show navigationbar end*/
    }
    
    private void switchColor(int i){
      switch (i) {
      case 0:
            images[0].setBackgroundColor(Color.WHITE);
         break;
      case 1:
          images[0].setBackgroundColor(Color.BLACK);
         break;
      case 2:
          images[0].setBackgroundColor(Color.RED);
         break;
      case 3:
          images[0].setBackgroundColor(Color.GREEN);
         break;
      case 4:
         images[0].setBackgroundColor(Color.BLUE);
         break;
      case 5:
         images[0].setVisibility(View.GONE);
         images[1].setVisibility(View.VISIBLE);
         images[2].setVisibility(View.VISIBLE);
         images[3].setVisibility(View.VISIBLE);
         images[4].setVisibility(View.VISIBLE);
         images[5].setVisibility(View.VISIBLE);
         images[6].setVisibility(View.VISIBLE);
         break;
      case 6:
          images[0].setVisibility(View.GONE);
          images[1].setVisibility(View.GONE);
          images[2].setVisibility(View.GONE);
          images[3].setVisibility(View.GONE);
          images[4].setVisibility(View.GONE);
          images[5].setVisibility(View.GONE);
          images[6].setVisibility(View.GONE);
          relativeLayout.setVisibility(View.VISIBLE);
          break;

      default:
         break;
      }
        
    }
}
